package cn.iocoder.yudao.module.erp.controller.admin.receiptconfig;

import cn.iocoder.yudao.module.erp.dal.dataobject.supplierfleet.SupplierFleetDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.erp.controller.admin.receiptconfig.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.receiptconfig.ReceiptConfigDO;
import cn.iocoder.yudao.module.erp.service.receiptconfig.ReceiptConfigService;

@Tag(name = "管理后台 - 客户回单配置")
@RestController
@RequestMapping("/erp/receipt-config")
@Validated
public class ReceiptConfigController {

    @Resource
    private ReceiptConfigService receiptConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建客户回单配置")
//    @PreAuthorize("@ss.hasPermission('erp:receipt-config:create')")
    public CommonResult<String> createReceiptConfig(@Valid @RequestBody ReceiptConfigSaveReqVO createReqVO) {
        return success(receiptConfigService.createReceiptConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户回单配置")
//    @PreAuthorize("@ss.hasPermission('erp:receipt-config:update')")
    public CommonResult<Boolean> updateReceiptConfig(@Valid @RequestBody ReceiptConfigSaveReqVO updateReqVO) {
        receiptConfigService.updateReceiptConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户回单配置")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('erp:receipt-config:delete')")
    public CommonResult<Boolean> deleteReceiptConfig(@RequestParam("id") String id) {
        receiptConfigService.deleteReceiptConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户回单配置")
//    @PreAuthorize("@ss.hasPermission('erp:receipt-config:query')")
    public CommonResult<ReceiptConfigRespVO> getReceiptConfig() {
        ReceiptConfigDO receiptConfig = receiptConfigService.getReceiptConfig();
        return success(BeanUtils.toBean(receiptConfig, ReceiptConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户回单配置分页")
//    @PreAuthorize("@ss.hasPermission('erp:receipt-config:query')")
    public CommonResult<PageResult<ReceiptConfigRespVO>> getReceiptConfigPage(@Valid ReceiptConfigPageReqVO pageReqVO) {
        PageResult<ReceiptConfigDO> pageResult = receiptConfigService.getReceiptConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ReceiptConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户回单配置 Excel")
//    @PreAuthorize("@ss.hasPermission('erp:receipt-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportReceiptConfigExcel(@Valid ReceiptConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReceiptConfigDO> list = receiptConfigService.getReceiptConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户回单配置.xls", "数据", ReceiptConfigRespVO.class,
                        BeanUtils.toBean(list, ReceiptConfigRespVO.class));
    }

}